var	WorldEditor	=	Class
({
	//	[CH]	初始化
	init	:	function ()
	{
		var	$M	=	this;
		if( is_undefined( this.dhtml ) || !is_object( this.dhtml ) )
			this.dhtml	=	new dhtml();

		if( is_undefined( this.widget ) || !is_object( this.widget ) )
			this.widget	=	new	widget();

		this.dhtml.loading( 'Welcome To <span>World Editor</span> ....' );
		this.dhtml.display( $( 'MDKloading' ) );

		if( $( 'nav_register' ) && $( 'nav_login' ) )
			$( 'nav_register' ).onclick = $( 'nav_login' ).onclick = function (e){ return $M.showWindow( this.id.substr( 4 ) ) };
		if( $( 'nav_loginout' ) )
			$( 'nav_loginout' ).onclick	=	function (e) { return $M.userLoginout( this.id.substr( 4 ) ) };
		if( $( 'open_forum_list_menu' ) )
			$( 'open_forum_list_menu' ).onclick	=	function (e) { return $M.forumCtl( 1 ) };
		if( $( 'close_forum_list_menu' ) )
			$( 'close_forum_list_menu' ).onclick	=	function (e) { return $M.forumCtl( 0 ) };

		/*if( $( 'forum_list' ).style.display == 'block' )
		{
			var	as	=	$$( 'a', $( 'forum_list' ) );
			for( var i = 0, j = as.length; i < j; i++ )
			{
				as[i].onclick	=	function (e) { return this.style.backgroundColor = '#ccc' };
				as[i].onmouseout	=	function (e) { return this.style.backgroundColor = '#fff' };
			}
		}*/
		this.setForumsMenu();

		this.showCityWeather();
	},

	//	[CH]	创建论坛列表动作
	setForumsMenu	:	function ()
	{
		var	$M		=	this;
		var	oImg	=	$$( 'img', $( 'forum_list' ) );
		for( var i = 0, j = oImg.length; i < j; i++ )
		{
			if( oImg[i].id.indexOf( 'forums_child_' ) != -1 )
				oImg[i].onclick	=	function (e) { return $M.showMenu( this.id.substr( 13 ) ) };
		}
	},

	showMenu		:	function ( pid )
	{
		var	oDiv	=	$$( 'div', $( 'forum_list' ) );
		//alert( pid );
		for( var i = 0, j = oDiv.length; i < j; i++ )
		{
			if( oDiv[i].id == 'forums_parent_' + pid )
			{
				oDiv[i].style.display	=	'block';
				//this.dhtml.setStyle( oDiv[i], { display : 'block', position : 'absolute', top : '200px', left : '200px' } );
			}
		}
	},

	//	[CH]	板块列表控制按钮动作
	forumCtl		:	function ( action )
	{
		//$( 'forum_list' ).style.display	=	$( 'forum_list' ).style.display == 'none' ? 'block' : 'none';
		//if( action )
		//	$( 'forum_list' ).style.display
		$( 'forum_list' ).style.display	=	action ? 'block' : 'none';
	},

	//	[CH]	用户退出
	userLoginout	:	function ( id )
	{
		var	$M	=	this;
		var	hash	=	this.widget.hash( 72 );
		var	bgDiv	=	!$( 'lightbox_bg' ) ? $M.dhtml.create( 'div', 'lightbox_bg' ) : $( 'lightbox_bg' );
		$M.dhtml.setStyle( bgDiv, { width : $M.dhtml.getPageWidth() + 'px', height : $M.dhtml.getPageHeight() + 'px', opacity : '0.6', display : 'block' } );


		var	loginout	=	function ()
		{
			if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
			{
				var	code	=	MDK.Ajax.xmlHttp.responseText.substr( 0, 6 );	//	[CH]	获取返回信息码
				var	msg		=	MDK.Ajax.xmlHttp.responseText.substr( 7 );		//	[CH]	获取返回信息
				var	sec		=	5;

				var	times	=	function ()
				{
					sec--;
					if( sec != 0 )
						var	timerId	=	window.setInterval( times, 1000 );
					else
					{
						clearInterval( timerId );
						location.reload( true );
					}
				}

				if( code == 'Err[0]' )
				{
					msg	=	'<a onclick="location.reload(true)">' + msg + '</a>';
					$M.dhtml.loading( msg );
					times();
				}
				else
				{
					msg	=	'<a onclick="$(\'MDKloading\').style.display=\'none\';$(\'bgDiv\').style.display=\'none\'">' + msg + '</a>';
					$M.dhtml.loading( msg );
				}
			}
			else
			{
				$M.dhtml.loading( '正在清理用户数据，请稍后...' );
			}
		}

		MDK.Ajax.init( 'post', './u/login.php', loginout, 'hash=' + hash );
	},

	//	[CH]	显示城市天气信息
	showCityWeather	:	function ()
	{
		if( !$( 'weather1' ) || !$( 'weather2' ) || !$( 'weather3' ) )
		return;

		var	showCityWeather	=	function ()
		{
			if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
			{
				//	[CH]	可能因为eval函数存在一定的危险性
				//	[CH]	部分浏览器可能屏蔽了该函数
				//	[CH]	这个是从庚庚的机器上发现的问题
				//	[CH]	待验证
				//	[CH]	2010-3-25
				//eval( "var arr = " + MDK.Ajax.xmlHttp.responseText );
				var	arr	=	MDK.Ajax.xmlHttp.responseText.split( '|' );
				$( 'weather1' ).innerHTML	=	arr[0];
				$( 'weather2' ).innerHTML	=	arr[1];
				$( 'weather3' ).innerHTML	=	arr[2];

				arr	=	null;
			}
		}
		MDK.Ajax.init( 'get', './u/weather.php', showCityWeather );
	},

	//	[CH]	创建注册与登录窗口
	showWindow	:	function ( id )
	{
		$M	=	this;

		MDK.include( 'md5' );
		MDK.include( 'user-' + id );

		//	[CH]	创建全局变量,用来储存注册验证信息
		window.auth	=	[];
		auth['email']	=	false;
		auth['username']	=	false;
		auth['password']	=	false;
		auth['authcode']	=	false;

		switch ( id )
		{
			case	'register'	:
			case	'login'	:
				if( $( 'me-' + id + '-window' ) )
				{
					$$( 'img', $( 'change-' + id + '-authcode' ) )[0].src = './kernel/authcode.h.php?authcode=' + new Date().getTime();
					$( 'lightbox_bg' ).style.display = 'block';
					return $( 'me-' + id + '-window' ).style.display = 'block';
				}

			break;
		}

		var	width	=	id == 'register' ? 332 : 302;
		var	height	=	id == 'register' ? 262 : 202;

		var	createWindow	=	function ()
		{
			if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
			{
				var	bgDiv	=	!$( 'lightbox_bg' ) ? $M.dhtml.create( 'div', 'lightbox_bg' ) : $( 'lightbox_bg' );
				var	windows	=	$M.dhtml.create( 'div', 'me-' + id + '-window', false, false, MDK.Ajax.xmlHttp.responseText );

				$M.dhtml.setStyle( bgDiv, { width : $M.dhtml.getPageWidth() + 'px', height : $M.dhtml.getPageHeight() + 'px', opacity : '0.6', display : 'block' } );
				$M.dhtml.setStyle( windows, { left : ( $M.dhtml.getClientWidth() - width ) / 2 + 'px', top : ( $M.dhtml.getClientHeight() - height ) / 2 + 'px' } );

				window.onresize	=	function (e)
				{
					$M.dhtml.setStyle( $( 'lightbox_bg' ), { width : $M.dhtml.getPageWidth() + 'px', height : $M.dhtml.getPageHeight() + 'px' } );
					$M.dhtml.setStyle( $( 'me-' + id + '-window' ), { left : ( $M.dhtml.getClientWidth() - width ) / 2 + 'px', top : ( $M.dhtml.getClientHeight() - height ) / 2 + 'px' } );
				};

				$( 'change-' + id + '-authcode' ).onclick	=	function (e)
				{
					$$( 'img', this )[0].src	=	'./kernel/authcode.h.php?authcode=' + new Date().getTime();
				};

				$( 'close-' + id + '-window' ).onclick	=	function (e)
				{
					$( 'lightbox_bg' ).style.display	=	'none';
					$( 'me-' + id + '-window' ).style.display	=	'none';
				}

				if( id == 'register' )
				{
					$( 'register-email' ).onchange	=	function (e) { return new weUserRegister( this, $M ).checkEmail() };
					$( 'register-username' ).onchange	=	function (e) { return new weUserRegister( this, $M ).checkUsername() };
					//$( 'register-password' ).onchange	=	function (e) { return new weUserRegister( this ).checkPassword() };
					$( 'register-authcode' ).onchange	=	function (e) { return new weUserRegister( this, $M ).checkAuthcode() };
					$( 'register-password' ).onchange	=	$( 'register-password2' ).onchange	=	function (e) { return new weUserRegister( this ).checkPassword() };
					$( 'do-register' ).onclick	=	function (e) { return new weUserRegister( this, $M ).checkReg() };
				}

				if( id == 'login' )
				{
					//	[CH]	似乎有些多余了
					/*$( 'login-username' ).onchange	=	function (e) { return new weUserLogin( this ).checkUsername() };
					$( 'login-password' ).onchange	=	function (e) { return new weUserLogin( this ).checkPassword() };*/
					$( 'do-login' ).onclick	=	function (e) { return new weUserLogin( this, $M ).checkLogin() };
				}

				width	=	height	=	windows	=	bgDiv	=	null;
			}
		}

		MDK.Ajax.init( 'get', './u/Ajax.php?do=' + id, createWindow );
	}
})